<?php 

namespace Acme\UserBundle\Form\EventListener;

use Symfony\Component\Form\Event\DataEvent;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvents;

class AddNameFieldSubscriber implements EventSubscriberInterface
{
	private $factory;

	public function __construct(FormFactoryInterface $factory)
	{
		$this->factory = $factory;
	}

	public static function getSubscribedEvents()
	{
		// Tells the dispatcher that we want to listen on the form.pre_set_data
		// event and that the preSetData method should be called.
		return array(FormEvents::PRE_SET_DATA => 'preSetData');
	}

	public function preSetData(DataEvent $event)
	{
		$data = $event->getData();
		$form = $event->getForm();

		// During form creation setData() is called with null as an argument
		// by the FormBuilder constructor. We're only concerned with when
		// setData is called with an actual Entity object in it (whether new,
		// or fetched with Doctrine). This if statement let's us skip right
		// over the null condition.
		if (null === $data) {
			return;
		}

		// check if the product object is "new"
		if (!$data->getId()) {
			$form->add($this->factory->createNamed('text', 'name'));
		}
	}
}
